#include "Daemon.hpp"
#include "OnlineCal.hpp"
#include "protocol.hpp"
#include "log.hpp"
#include "tcpserver.hpp"

using namespace MyLog;

// ./tcpserver port
int main(int argc, char *argv[])
{
    if(argc != 2){
        std::cerr << "Usage->" << argv[0] << "port" << std::endl;
        exit(USAGE_ERR);
    }
    //std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]));

    USE_FILE_LOG_STARATRGY();
    std::cout << "tcpserver 守护进程化" << std::endl;
    // 服务器守护进程化
    Daemon(0, 0);

    // 将日志策略改为像指定目录下的指定文件打印

    // 1.上层 计算器服务
    std::unique_ptr<onliecal> ocal = std::make_unique<onliecal>();

    // 2.定制协议
    std::unique_ptr<protocol> p = std::make_unique<protocol>([&ocal](Request &req)->Response{
        return ocal->Deal(req);
    });

    // 3.启动服务器
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]), 
        [&p](std::shared_ptr<Socket> &sock, InetAddr &addr){
            p->GetRequest(sock, addr);
        });


    tsvr->Start();
    return 0;
}